package com.kaifamiao.service.Impl;

import com.kaifamiao.mapper.UserMapper;
import com.kaifamiao.model.UserModel;
import com.kaifamiao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service()
@Transactional(propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    @Override
    public List<UserModel> findAll() {
        return mapper.selectAll();
    }

    @Override
    public UserModel findById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int addUser(UserModel model) {
        Long id=model.getId();
        String accountName=model.getAccountname();
        String password=model.getPassword();
        String name=model.getName();
        Integer logTimes=model.getLogtimes();
        String sex=model.getSex();
        String touXiang=model.getTouxiang();
        String regdate=model.getRegdate();
        String phone=model.getPhone();
        Integer roleId=model.getRoleId();
        model.setId(id);
        model.setAccountname(accountName);
        model.setPassword(password);
        model.setName(name);
        model.setLogtimes(logTimes);
        model.setSex(sex);
        model.setTouxiang(touXiang);
        model.setRegdate(regdate);
        model.setPhone(phone);
        model.setRoleId(roleId);
        return mapper.insertSelective(model);
    }

    @Override
    public int updateUser(UserModel model) {
        Long id=model.getId();
        String accountName=model.getAccountname();
        String password=model.getPassword();
        String name=model.getName();
        Integer logTimes=model.getLogtimes();
        String sex=model.getSex();
        String touXiang=model.getTouxiang();
        String regdate=model.getRegdate();
        String phone=model.getPhone();
        Integer roleId=model.getRoleId();
        model.setId(id);
        model.setAccountname(accountName);
        model.setPassword(password);
        model.setName(name);
        model.setLogtimes(logTimes);
        model.setSex(sex);
        model.setTouxiang(touXiang);
        model.setRegdate(regdate);
        model.setPhone(phone);
        model.setRoleId(roleId);
        return mapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public int deleteUser(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UserModel> findByUser(UserModel model) {
        String accountname=model.getAccountname();
        String password=model.getPassword();
        model.setAccountname(accountname);
        model.setPassword(password);
        return mapper.selectByUser(model);
    }

    @Override
    public UserModel selectByAccountName(String accountname) {
        return mapper.selectByAccountName(accountname);
    }

    @Override
    public int addOid(Integer oid, Integer id) {
        return mapper.addOid(oid, id);
    }

    @Override
    public int setOidNull(Integer id) {
        return mapper.setOidNull(id);
    }
}
